﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using Rizmar.RizmarDataSetTableAdapters;

namespace Rizmar
{
    public partial class UsuarioForm : Form
    {
        public UsuarioForm()
        {
            InitializeComponent();
        }

        private void cancelarButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            UsuarioTableAdapter uta = new UsuarioTableAdapter();
            if (uta.Insert(usuarioTextBox.Text, geramd5(senhaTextBox.Text), adminCheckBox.Checked, true) > 0) 
                MessageBox.Show("Usuário cadastrado com sucesso!");
        }

        public string geramd5(string input)
        {
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
                sb.Append(hash[i].ToString("x2"));
            return sb.ToString();
        }

        
        public void setUserName(string username) { usuarioTextBox.Text = username; }
        public void setPassword(string password) { senhaTextBox.Text = password; }
        public void setAdmin(bool admin) { adminCheckBox.Checked = admin; }

        
    }
}
